﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Linq;
using ASER.DataAccess.BaseDataAccess;
using ASER.DataEntity.Security;

namespace DataAccess.Platform
{
    /// <summary>
    /// 数据访问类:ApplicationDataAccess
    /// 文  件  名:ApplicationDataAccess.cs
    /// 说      明:应用程序管理数据访问对象
    /// </summary>
    public class ApplicationDataAccess : BaseDataAccess<ApplicationEntity>
    {
        #region 构造器

        public ApplicationDataAccess()
        {

        }

        #endregion

        #region 公共方法

        /// <summary>
        ///获取应用程序管理所有数据
        /// </summary>
        public List<ApplicationEntity> GetApplications()
        {
            List<ApplicationEntity> Result = null;
            string sqlText = @"SELECT * FROM  Application";
            using (DbCommand Command = DataBase.GetSqlStringCommand(sqlText))
            {
                IDataReader Reader = DataBase.ExecuteReader(Command);
                try
                {
                    Result = FillDataListGeneric(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }

        /// <summary>
        /// 获取应用程序管理数据
        /// </summary>
        /// <param name="ApplicationNo">应用程序编号</param> 
        /// <returns>应用程序管理实体</returns>
        public ApplicationEntity GetApplication(string ApplicationNo)
        {
            string SqlText = "SELECT * FROM  Application WHERE ApplicationNo=@ApplicationNo ";
            ApplicationEntity Result = null;
            using (DbCommand Command = DataBase.GetSqlStringCommand(SqlText))
            {
                DataBase.AddInParameter(Command, "@ApplicationNo", DbType.AnsiString, ApplicationNo);
                IDataReader Reader = DataBase.ExecuteReader(Command);
                try
                {
                    Result = FillEntities(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }

        /// <summary>
        /// 保存应用程序管理数据
        /// </summary>
        /// <param name="Application">应用程序管理实体</param>        
        /// <returns>成功保存的行数</returns>
        public int SaveApplication(ApplicationEntity Application)
        {
            int Result = 0;
            if (Exists(Application))
            {
                Result = UpdateApplication(Application);
            }
            else
            {
                Result = AddApplication(Application);
            }
            return Result;
        }

        /// <summary>
        /// 删除应用程序管理数据
        /// </summary>
        /// <param name="Application">应用程序管理实体</param>        
        /// <returns>删除的数据行数</returns>
        public int DeleteApplication(ApplicationEntity Application)
        {
            int Result = 0;
            string SqlText = @"DELETE FROM Application WHERE ApplicationNo=@ApplicationNo ";
            using (DbCommand Command = DataBase.GetSqlStringCommand(SqlText))
            {
                FillDataParameters(Command, Application);
                Result = DataBase.ExecuteNonQuery(Command);
            }
            return Result;
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 新增应用程序管理数据
        /// </summary>
        /// <param name="Application">应用程序管理实体</param>        
        /// <returns>新增的数据行数</returns>
        private int AddApplication(ApplicationEntity Application)
        {
            int Result = 0;
            string SqlText = @"Insert Application (ApplicationNo,ApplicationName,ApplicationURL) Values (@ApplicationNo,@ApplicationName,@ApplicationURL) ";
            using (DbCommand Command = DataBase.GetSqlStringCommand(SqlText))
            {
                FillDataParameters(Command, Application);
                Result = DataBase.ExecuteNonQuery(Command);
            }
            return Result;
        }


        /// <summary>
        /// 更新应用程序管理数据
        /// </summary>
        /// <param name="Application">应用程序管理实体</param>        
        /// <returns>更新的数据行数</returns>
        private int UpdateApplication(ApplicationEntity Application)
        {
            int Result = 0;
            string SqlText = @"Update Application Set ApplicationName=@ApplicationName , ApplicationURL=@ApplicationURL  Where ApplicationNo = @ApplicationNo ";
            using (DbCommand Command = DataBase.GetSqlStringCommand(SqlText))
            {
                FillDataParameters(Command, Application);
                Result = DataBase.ExecuteNonQuery(Command);
            }
            return Result;
        }


        /// <summary>
        /// 判断应用程序管理数据是否存在
        /// </summary>
        /// <param name="Application">应用程序管理实体</param>        
        /// <returns>数据存在则返回true否则返回false</returns>
        private bool Exists(ApplicationEntity Application)
        {
            bool Result = false;
            string SqlText = "SELECT * FROM  Application WHERE ApplicationNo=@ApplicationNo ";
            using (DbCommand Command = DataBase.GetSqlStringCommand(SqlText))
            {
                DataBase.AddInParameter(Command, "@ApplicationNo", DbType.AnsiString, Application.ApplicationNo);
                IDataReader Reader = DataBase.ExecuteReader(Command);
                try
                {
                    Result = Reader.Read();
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }

        #endregion

        #region IDisposable 成员

        public override void Dispose()
        {

        }

        #endregion
    }
}
